package com.cn.shuangzi.ad.custom;

import android.content.Context;
import android.view.View;

import com.bytedance.sdk.openadsdk.mediation.MediationConstant;
import com.bytedance.sdk.openadsdk.mediation.ad.IMediationDislikeCallback;
import com.bytedance.sdk.openadsdk.mediation.bridge.custom.native_ad.MediationCustomNativeAd;
import com.cn.shuangzi.ad.ADManager;
import com.cn.shuangzi.util.SZUtil;
import com.kwad.sdk.api.KsFeedAd;

/**
 * 快手 信息流 ADN提供渲染（模板渲染）广告对象
 */
public class KsFeedNativeExpressAd extends MediationCustomNativeAd {
    private KsFeedAd mKsFeedAd;
    IMediationDislikeCallback mDislikeCallback; //不喜欢回调
    private Context mContext;
    private View mExpressView;


    public KsFeedNativeExpressAd(Context context, KsFeedAd feedAd) {
        this.mKsFeedAd = feedAd;
        this.mContext = context;
        ADManager.getInstance().setKsFeedAd(feedAd);
        if (isClientBidding()) {//bidding广告类型
            double ecpm = feedAd.getECPM();//当无权限调用该接口时，SDK会返回错误码-1
            if (ecpm < 0) {
                ecpm = 0;
            }
            setBiddingPrice(ecpm);
        }

        setExpressAd(true);
        this.mKsFeedAd.setAdInteractionListener(new KsFeedAd.AdInteractionListener() {
            @Override
            public void onAdClicked() {
                SZUtil.logError( "快手信息流，onADClicked");
                callAdClick();
            }

            @Override
            public void onAdShow() {
                SZUtil.logError( "快手信息流，onADExposed");
                callAdShow();
//                callNativeAdShow();
            }

            @Override
            public void onDislikeClicked() {
                SZUtil.logError("=====快手信息流Dislike关闭=====");
                if (mDislikeCallback != null) {
                    mDislikeCallback.onSelected(-1, "ks信息流模板dislike接口无关闭原因");
                }
                callDislikeSelected(-1, "快手信息流模板dislike接口无关闭原因");
            }

            @Override
            public void onDownloadTipsDialogShow() {
            }

            @Override
            public void onDownloadTipsDialogDismiss() {

            }
        });
    }

    @Override
    public void render() {
        super.render();
        SZUtil.logError( "快手信息流render");
        if (mKsFeedAd != null) {
            mExpressView = mKsFeedAd.getFeedView(mContext);
            if (mExpressView != null) {
                callRenderSuccess(MediationConstant.FULL_WIDTH, MediationConstant.AUTO_HEIGHT);
            } else {
                callRenderFail(null,111 , "render fail");
            }
        } else {
            callRenderFail(null, 111, "render fail");
        }
    }

    @Override
    public View getExpressView() {
        return mExpressView;
    }
}